package com.authine.cloudpivot.ext.utils.risk;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.constants.RiskRaiderConstant;
import com.authine.cloudpivot.ext.dto.risk.RiskSaasAccountData;
import com.authine.cloudpivot.ext.utils.DESUtil;
import com.authine.cloudpivot.ext.utils.HttpsClientUtil;

public class RiskRaiderUtils {

	static final String apiKey = RiskRaiderConstant.API_KEY;
	static final String accountId = RiskRaiderConstant.ACCOUNT_ID;
	static final String accountName = RiskRaiderConstant.ACCOUNT_NAME;
	static final String accountData = RiskRaiderConstant.ACCOUNT_DATA;

	/**
	 * 查询目标客户下的所有风险雷达SaaS端账号
	 * 
	 * @return
	 */
	public static String getSaaSAccount() {
		try {
			String encryptName = DESUtil.encrypt(accountData);
			Map<String, Object> bodys = new HashMap<>(4);
			bodys.put("userName", accountName);
			bodys.put("apiKey", encryptName);
			String res = HttpsClientUtil.doPost(RiskRaiderConstant.GET_SAAS_ACCOUNT, JSON.toJSONString(bodys), "utf-8");
			if (res == null || "".equals(res)) {
				throw new RuntimeException("查询目标客户下的所有风险雷达SaaS端账号失败！");
			}
			RiskSaasAccountData resEntity = JSON.parseObject(res, RiskSaasAccountData.class);
			if (!"0".equals(resEntity.getResultCode())) {
				throw new RuntimeException("获取SaaS账号信息失败！");
			}
			return resEntity.getResultData().get(0).getAccountId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("接口：getSaaSAccount，请求异常:");
		}
	}

	/**
	 * 标准接口规范_企业风险扫描结果 3.0
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object enterRiskScan(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			bodys.put("accountId", getSaaSAccount());
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业舆情信息V3.0
	 * 
	 * @param url           接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName   公司名称
	 * @param newsTags      新闻标签(多个以逗号隔开)
	 * @param currentPageNo 当前页
	 * @param pageSize      每页条数
	 * @return
	 */
	public static Object enterRiskNews(String url, String companyName, String newsTags, int currentPageNo,
			int pageSize) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			bodys.put("newsTags", newsTags);
			bodys.put("currentPageNo", currentPageNo);
			bodys.put("pageSize", pageSize);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业经营分析V3.0
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object enterRiskFinance(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业真实性校验V3.0
	 * 
	 * @param url           接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName   公司名称
	 * @param certifyNumber 统一社会信用代码
	 * @return
	 */
	public static Object enterRiskTruth(String url, String companyName, String certifyNumber) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("certifyNumber", certifyNumber);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业搜索V3.1
	 * 
	 * @param url      接口地址，地址常量在RiskRaiderConstant中
	 * @param keywords (支持社会统一信用代码搜索)关键字
	 * @return
	 */
	public static Object enterRiskSearch(String url, String keywords) {
		try {
			keywords = DESUtil.encrypt(keywords);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("keywords", keywords);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业工商信息V3.1
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object enterRiskBusinessInfo(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业失信信息V3.1
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object enterRiskDiscreditInfo(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业司法裁判文书V3.0
	 * 
	 * @param url           接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName   公司名称
	 * @param currentPageNo 当前分页页数
	 * @return
	 */
	public static Object enterRiskJudgeInfo(String url, String companyName, int currentPageNo) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			bodys.put("currentPageNo", currentPageNo);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业司法全景V3.0
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object enterRiskJudicialPanorama(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 风险雷达标准接口规范_企业司法信息V3.0
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object enterRiskJusticeInfo(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 监控企业关系挖掘接口
	 * 
	 * @param url          接口地址，地址常量在RiskRaiderConstant中
	 * @param companyNames 公司名称集合
	 * @return
	 */
	public static Object getNewRelationDiagram(String url, List<String> companyNames) {
		try {
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("accountId", getSaaSAccount());
			bodys.put("companyNames", companyNames);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 监控企业更新事件企业列表接口
	 * 
	 * @param url                   接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName           企业名称
	 * @param accountType           SaaS账号类型
	 * @param pageSize              每页条数
	 * @param currentPageNo         当前页
	 * @param endDate               日期（格式为yyyy-MM-dd）
	 * @param monitorCycle          监控周期(1：日 2：周 3：月)
	 * @param riskSituation         风险状况（0：正常 1：关注 2：一般预警 3：特别预警 ）
	 * @param eventLevel            风险事件等级（1：一般 2：异常 3：严重）
	 * @param associatedEnterprises 关系企业/自然人(0：无 1：有)
	 * @param riskChange            风险变化（0：不变 1：升高 2：降低）
	 * @param tagList               事件标签（见事件字典接口）
	 * @return
	 */
	public static Object getMonitorEnterpriseEventUpdateInfo(String url, String companyName, String accountType,
			int pageSize, int currentPageNo, String endDate, String monitorCycle, String riskSituation,
			String eventLevel, String associatedEnterprises, String riskChange, String tagList) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("accountId", getSaaSAccount());
			bodys.put("enterpriseName", enterpriseName);
			bodys.put("accountType", accountType);
			bodys.put("pageSize", pageSize);
			bodys.put("currentPageNo", currentPageNo);
			bodys.put("endDate", endDate);
			bodys.put("monitorCycle", monitorCycle);
			bodys.put("riskSituation", riskSituation);
			bodys.put("eventLevel", eventLevel);
			bodys.put("associatedEnterprises", associatedEnterprises);
			bodys.put("riskChange", riskChange);
			bodys.put("tagList", tagList);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 监控企业更新事件详情接口(在调用此接口前需调用（标准监控企业更新事件企业列表接口）)
	 * 
	 * @param url            接口地址，地址常量在RiskRaiderConstant中
	 * @param eventSubType   事件子类型 (控企业更新事件企业列表接口返回结果)
	 * @param monitorId      监控企业id
	 * @param pageSize       每页条数
	 * @param currentPageNo  当前页
	 * @param eventScopeFlag 事件范围标志（1：全部事件，2：更新事件范围内的事件）
	 * @param endDate        查询日期（格式为 yyyy-MM-dd）
	 * @param monitorCycle   监控周期(1：日 2：周 3：月)
	 * @return
	 */
	public static Object getEventDetail(String url, String eventSubType, String monitorId, int pageSize,
			int currentPageNo, String eventScopeFlag, String endDate, String monitorCycle) {
		try {
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("eventSubType ", eventSubType);
			bodys.put("monitorId ", monitorId);
			bodys.put("accountId", getSaaSAccount());
			bodys.put("pageSize", pageSize);
			bodys.put("currentPageNo", currentPageNo);
			bodys.put("eventScopeFlag", eventScopeFlag);
			bodys.put("endDate", endDate);
			bodys.put("monitorCycle", monitorCycle);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 监控企业风险信息
	 * 
	 * @param url         接口地址，地址常量在RiskRaiderConstant中
	 * @param companyName 公司名称
	 * @return
	 */
	public static Object getMonitorInfoOfEnterprise(String url, String companyName) {
		try {
			String enterpriseName = DESUtil.encrypt(companyName);
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("enterpriseName", enterpriseName);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * SaaS账户下监控企业列表接口--》企业监控列表接口（带分页)
	 * 
	 * @param url           接口地址，地址常量在RiskRaiderConstant中
	 * @param pageSize      每页条数
	 * @param currentPageNo 当前页
	 * @return
	 */
	public static Object getEnterpriseList(String url, int pageSize, int currentPageNo) {
		try {
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("accountId", getSaaSAccount());
			bodys.put("pageSize", pageSize);
			bodys.put("currentPageNo", currentPageNo);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 公司下监控企业列表
	 * 
	 * @param url           接口地址，地址常量在RiskRaiderConstant中
	 * @param companyIdList 公司ID列表
	 * @return
	 */
	public static Object getMonitorEnterpriseListByClientCompany(String url, List<String> companyIdList) {
		try {
			Map<String, Object> bodys = new HashMap<String, Object>();
			bodys.put("userName", accountName);
			bodys.put("clientCompanyIds", companyIdList);
			String res = HttpsClientUtil.doPost(url, JSON.toJSONString(bodys), "utf-8");
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static void main(String[] args) {
		// 标准接口规范_企业风险扫描结果 3.0
		// enterRiskScan(RiskRaiderConstant.FULL_ENTER_RISK, "深圳奥哲网络科技有限公司");

		// 风险雷达标准接口规范_企业舆情信息V3.0
		// enterRiskNews(RiskRaiderConstant.ENTER_NEWS,"深圳奥哲网络科技有限公司", null, 1, 20);

		// 风险雷达标准接口规范_企业经营分析V3.0
		// enterRiskFinance(RiskRaiderConstant.FUL_ENTER_FINANCE, "深圳奥哲网络科技有限公司");

		// 风险雷达标准接口规范_企业真实性校验V3.0
		// enterRiskTruth(RiskRaiderConstant.ENTER_TRUTH,
		// "深圳奥哲网络科技有限公司","91440300555416544T");

		// 风险雷达标准接口规范_企业搜索V3.1
		// enterRiskSearch(RiskRaiderConstant.ENTER_SEARCH, "深圳奥哲");

		// 风险雷达标准接口规范_企业工商信息V3.1
		// enterRiskBusinessInfo(RiskRaiderConstant.MONITOR_ENTER_BUSINESS_INFO,
		// "深圳奥哲网络科技有限公司");

		// 风险雷达标准接口规范_企业失信信息V3.1
		// enterRiskDiscreditInfo(RiskRaiderConstant.MONITOR_ENTER_DISCREDIT_INFO,
		// "深圳奥哲网络科技有限公司");

		// 风险雷达标准接口规范_企业司法裁判文书V3.0
		// enterRiskJudgeInfo(RiskRaiderConstant.FULL_ENTER_JUDGE_INFO, "深圳奥哲网络科技有限公司",
		// 1);

		// 风险雷达标准接口规范_企业司法全景V3.0
		// enterRiskJudicialPanorama(RiskRaiderConstant.FULL_ENTER_JUDICIAL_PANORAMA,
		// "深圳奥哲网络科技有限公司");

		// 风险雷达标准接口规范_企业司法信息V3.0
		// enterRiskJusticeInfo(RiskRaiderConstant.FULL_ENTER_JUSTICE_INFO,
		// "深圳奥哲网络科技有限公司");

		// getSaaSAccount();
		/*
		 * List<String> companyNames = new ArrayList<String>();
		 * companyNames.add("深圳奥哲网络科技有限公司");
		 * getNewRelationDiagram(RiskRaiderConstant.NEW_RELATION_DIAGRAM, companyNames);
		 */
	}

}
